package org.jeecg.modules.attendance.service.impl;

import org.jeecg.modules.attendance.entity.AgAttendance;
import org.jeecg.modules.attendance.entity.AgAttendanceRecord;
import org.jeecg.modules.attendance.mapper.AgAttendanceRecordMapper;
import org.jeecg.modules.attendance.mapper.AgAttendanceMapper;
import org.jeecg.modules.attendance.service.IAgAttendanceService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 考勤
 * @Author: jeecg-boot
 * @Date:   2020-05-26
 * @Version: V1.0
 */
@Service
public class AgAttendanceServiceImpl extends ServiceImpl<AgAttendanceMapper, AgAttendance> implements IAgAttendanceService {

	@Autowired
	private AgAttendanceMapper agAttendanceMapper;
	@Autowired
	private AgAttendanceRecordMapper agAttendanceRecordMapper;
	
	@Override
	@Transactional
	public void saveMain(AgAttendance agAttendance, List<AgAttendanceRecord> agAttendanceRecordList) {
		agAttendanceMapper.insert(agAttendance);
		if(agAttendanceRecordList!=null && agAttendanceRecordList.size()>0) {
			for(AgAttendanceRecord entity:agAttendanceRecordList) {
				//外键设置
				entity.setAttendanceId(agAttendance.getId());
				agAttendanceRecordMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(AgAttendance agAttendance,List<AgAttendanceRecord> agAttendanceRecordList) {
		agAttendanceMapper.updateById(agAttendance);
		
		//1.先删除子表数据
		agAttendanceRecordMapper.deleteByMainId(agAttendance.getId());
		
		//2.子表数据重新插入
		if(agAttendanceRecordList!=null && agAttendanceRecordList.size()>0) {
			for(AgAttendanceRecord entity:agAttendanceRecordList) {
				//外键设置
				entity.setAttendanceId(agAttendance.getId());
				agAttendanceRecordMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		agAttendanceRecordMapper.deleteByMainId(id);
		agAttendanceMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			agAttendanceRecordMapper.deleteByMainId(id.toString());
			agAttendanceMapper.deleteById(id);
		}
	}
	
}
